import WeChatBot from './wechatbot.js';
import utils from './base.js';

export default function(config, loop = 30000) {
  const bot = new WeChatBot(config);
  let alerts = {};
  return {
    add: async (key, message) => {
      alerts[key] = message;
    },
    remove: async (key) => {
      delete alerts[key];
    },
    bot,
    run: async () => {
      do {
        await utils.sleep(loop);
        let messages = [];
        for (const alert in alerts) {
          messages.push(alerts[alert]);
        }
        if (messages.length > 0) {
          const message = messages.join('\n');
          await bot.sendMsg(message);
        }
      } while (true);
    },
    sendMsg: bot.sendMsg,
  };

}
